#include "color.h"		//显示颜色的头文件
#include "bmp.h"		//显示bmp图片的头文件
#include "ts.h"			//触摸屏的头文件
#include "music.h"		//播放音乐所需的头文件
#include "movie.h"		//播放视频所需的头文件
#include "rfid.h"
#include "unistd.h"
#include "stdlib.h"
#include "unistd.h"
#include "pthread.h"
#include "string.h"




char musicimage1[21][32]= {"./1/51.bmp","./1/52.bmp","./1/53.bmp","./1/54.bmp","./1/55.bmp","./1/56.bmp","./1/57.bmp","./1/58.bmp","./1/59.bmp","./1/60.bmp","./1/61.bmp","./1/62.bmp","./1/63.bmp","./1/64.bmp","./1/65.bmp","./1/66.bmp","./1/67.bmp","./1/68.bmp","./1/69.bmp","./1/70.bmp"};
char musicimage2[21][32]= {"./1/71.bmp","./1/72.bmp","./1/73.bmp","./1/74.bmp","./1/75.bmp","./1/76.bmp","./1/77.bmp","./1/78.bmp","./1/79.bmp","./1/80.bmp","./1/81.bmp","./1/82.bmp","./1/83.bmp","./1/84.bmp","./1/85.bmp","./1/86.bmp","./1/87.bmp","./1/88.bmp","./1/89.bmp","./1/90.bmp"};
//char musicimage1[3][16]= {"./1/51.bmp","./1/52.bmp","./1/53.bmp"};
//char musicimage2[3][16]= {"./1/71.bmp","./1/72.bmp","./1/73.bmp"};
int nows =0;
int music_plays_switch =0;



void music_plays_switch_kj(){
	if(music_plays_switch == 0)
	{
		music_plays_switch =1;
	}else{
		music_plays_switch =0;
	}
}

void music_plays()
{
	printf("kooook");
	while(flag_show_char)
	{
		if(flag_show_char == 1)
		{
			if(music_plays_switch == 0){
				show_bmp_hh(musicimage1[nows%20],292,104,200,200);
			}
			if(music_plays_switch == 1){
				show_bmp_hh(musicimage2[nows%20],292,104,200,200);
			}
				sleep(1);
				nows++;
			printf("%d/n",nows);
		}
	}
}


void* thread_function(void *arg)
{
    printf("New thread is running, argument is %s\n", (char *)arg);
	show_char();
}

void* thread_function2(void *arg)
{
    printf("New thread is running, argument is %s\n", (char *)arg);
	music_plays();
}



int main(int argc, char const *argv[])
{
	int vo_i=0;
	int ts_x,ts_y,fx;
	int con=0;
	int cardid;
	int tty1;
	char musicname[4][64]= {"./1.mp3","./2.mp3","./3.mp3","./4.mp3"};
	char musicname1[4][64]= {"1.mp3","2.mp3","3.mp3","4.mp3"};
	
	char voname[4][64]= {"./play.mp4","./2.mp4","./3.mp4","./4.mp4"};
	char voname1[4][64]= {"play.mp4","2.mp4","3.mp4","4.mp4"};

	char buff_start[128]= {0};

	int musicflag = 0;
	int musicflaggoto = 0;
	int music_ps_flag = 0;

	int music_switch = 0;

	int voflaggoto =0;
	int voswitch = 0;
	int vo_flag = 0;

	int double_ts = 0;

	int password=0;


	pthread_t thread_id;
    int ret;
    void *thread_result;
	//open_lcd_color();				//1、打开显示屏(color)
	open_lcd_bmp();					//1、打开显示屏(bmp)
	open_ts();						//1、打开触摸屏
	//1、打开串口1
	tty1 = open("/dev/ttySAC1",O_RDWR|O_NOCTTY);
	if (tty1 == -1){perror("open tty1 failed");return -1;}
	//2、串口初始化
	tty_init(tty1);
	//4、发送B命令，获取范围卡号最大的卡号的信息
	//show_color(0x0000ff00);				//2、显示颜色
	show_bmp("./0.bmp");				//2、显示bmp图片
	while(1)
	{
		sendA:
		rfid_send_A(tty1);
		cardid = rfid_send_B(tty1);
		if (cardid == -1)
		{
			goto sendA;
		}
		printf("此卡的ID为：%#x\n", cardid);
		if(cardid == 0x3d623bd9)
		{
			goto starting;
		}else
		{
			con++;
			if(con ==3)
			{
				show_bmp("./0_2.bmp");
				goto jx;
			}
			if(con ==2)
			{
				show_bmp("./0_4.bmp");
			}
			if(con ==1)
			{
				show_bmp("./0_1.bmp");
			}

			sleep(1);
		}
	}
	jx:
	while (1)
	{
		get_ts_xy(&ts_x, &ts_y);
		if(ts_x>0 && ts_x<800 && ts_y>0 && ts_y<480)
		{
			show_bmp("./0_3.bmp");
			while (1)
			{
				get_ts_xy(&ts_x, &ts_y);
				//1
				if(ts_x>205 && ts_x<288 && ts_y>240 && ts_y<306)
				{
					password = (password+1)*10;
					sprintf(buff_start,"%d",password/10);
					show_strings(buff_start);
				}
				//2
				if(ts_x>309 && ts_x<390 && ts_y>240 && ts_y<306)
				{
					password = (password+2)*10;
					sprintf(buff_start,"%d",password/10);
					show_strings(buff_start);
				}
				//3
				if(ts_x>412 && ts_x<494 && ts_y>240 && ts_y<306)
				{
					password = (password+3)*10;
					sprintf(buff_start,"%d",password/10);
					show_strings(buff_start);
				}
				//X
				if(ts_x>515 && ts_x<600 && ts_y>240 && ts_y<306)
				{
					password = password/10;
					sprintf(buff_start,"%d",password/10);
					show_strings(buff_start);
				}
				/*****************************************************************/
				//4
				if(ts_x>205 && ts_x<288 && ts_y>321 && ts_y<387)
				{
					password = (password+4)*10;
					sprintf(buff_start,"%d",password/10);
					show_strings(buff_start);
				}
				//5
				if(ts_x>309 && ts_x<390 && ts_y>321 && ts_y<387)
				{
					password = (password+5)*10;
					sprintf(buff_start,"%d",password/10);
					show_strings(buff_start);
				}
				//6
				if(ts_x>412 && ts_x<494 && ts_y>321 && ts_y<387)
				{
					password = (password+6)*10;
					sprintf(buff_start,"%d",password/10);
					show_strings(buff_start);
				}
				//0
				if(ts_x>515 && ts_x<600 && ts_y>321 && ts_y<387)
				{
					password = (password+0)*10;
					sprintf(buff_start,"%d",password/10);
					show_strings(buff_start);
				}

				/*****************************************************************/
				//7
				if(ts_x>205 && ts_x<288 && ts_y>399 && ts_y<466)
				{
					password = (password+7)*10;
					sprintf(buff_start,"%d",password/10);
					show_strings(buff_start);
				}
				
				//8
				if(ts_x>309 && ts_x<390 && ts_y>399 && ts_y<466)
				{
					password = (password+8)*10;
					sprintf(buff_start,"%d",password/10);
					show_strings(buff_start);
				}
				
				//9
				if(ts_x>412 && ts_x<494 && ts_y>399 && ts_y<466)
				{
					password = (password+9)*10;
					sprintf(buff_start,"%d",password/10);
					show_strings(buff_start);
				}
				
				//ok
				if(ts_x>515 && ts_x<600 && ts_y>399 && ts_y<466)
				{
					if(1234 == password/10)
					{
						goto starting;
					}
					else{
						password = 0;
						show_strings("password error!");
					}
				}
			}
		}
	}



	starting:
	show_bmp("./1.bmp");
	while (1)
	{
		get_ts_xy(&ts_x, &ts_y);
		//一、音乐播放器
		if(ts_x>149 && ts_x<239 && ts_y>141 && ts_y<259)
		{
			show_bmp("./2.bmp");
			while (1)
			{
				get_ts_xy(&ts_x, &ts_y);
				if (ts_x>0 && ts_x<50 && ts_y>0 && ts_y<50)
				{
					show_bmp("./1.bmp");
					break;
				}

				if (ts_x>60 && ts_x<520 && ts_y>89 && ts_y<165)
				{
					musicflag = 0;
					musicflaggoto =1;
				}
				if (ts_x>60 && ts_x<520 && ts_y>187 && ts_y<263)
				{
					musicflag = 1;
					musicflaggoto =1;
				}
				if (ts_x>60 && ts_x<520 && ts_y>279 && ts_y<359)
				{
					musicflag = 2;
					musicflaggoto =1;
				}
				if (ts_x>60 && ts_x<520 && ts_y>369 && ts_y<451)
				{
					musicflag = 3;
					musicflaggoto =1;
				}
				if(musicflaggoto == 1)
				{
					show_bmp("./4.bmp");
					while (1)
					{
						get_ts_xy(&ts_x, &ts_y);
						if (ts_x>0 && ts_x<50 && ts_y>0 && ts_y<50) //退出
						{
							musicflaggoto = 0;
							flag_show_char = 0;
							show_bmp("./2.bmp");
							break;
						}
						if (ts_x>293 && ts_x<357 && ts_y>414 && ts_y<480)
						{
							play_music(musicname[musicflag]);
							show_string(musicname1[musicflag],2);
							flag_show_char = 1;
							//show_char();
							flag_gc = musicflag%3;
							ret = pthread_create(&thread_id, NULL, thread_function, (void *)musicname[4]);
							if(ret != 0){
								printf("Create thread error! \n");
								exit(1);
							}
							ret = pthread_create(&thread_id, NULL, thread_function2, (void *)musicname[4]);
							if(ret != 0){
								printf("Create thread error! \n");
								exit(1);
							}
						}
						if (ts_x>185 && ts_x<230 && ts_y>414 && ts_y<480)
						{
							exit_music();
							flag_show_char = 0;
						}
						if (ts_x>372 && ts_x<418 && ts_y>414 && ts_y<480)
						{
							if(music_ps_flag == 0)
							{
								music_ps_flag =1;
								flag_show_char = 2;
								stop_music();
							}else{
								music_ps_flag =0;
								flag_show_char = 1;
								cont_music();
							}
						}
						if (ts_x>125 && ts_x<172 && ts_y>414 && ts_y<480)
						{
							while_music(musicname[musicflag]);
						}
						if (ts_x>546 && ts_x<588 && ts_y>414 && ts_y<480)
						{
							add_music(musicname[musicflag]);
						}
						if (ts_x>484 && ts_x<530 && ts_y>414 && ts_y<480)
						{
							sub_music(musicname[musicflag]);
						}
						if (ts_x>632 && ts_x<681 && ts_y>414 && ts_y<480)
						{
							flag_show_char = 0;
							exit_music();
							music_plays_switch_kj();
							show_bmp("./4-1.bmp");
							show_hemo(musicname1[(music_switch)%4],musicname1[(music_switch+1)%4],musicname1[(music_switch+2)%4]);
							while(1)
							{
								get_ts(&ts_x,&ts_y,&fx);
								if(fx == 1)
								{
									music_switch++;
									show_hemo(musicname1[(music_switch)%4],musicname1[(music_switch+1)%4],musicname1[(music_switch+2)%4]);
								}
								if(fx == 2)
								{
									music_switch--;
									show_hemo(musicname1[(music_switch)%4],musicname1[(music_switch+1)%4],musicname1[(music_switch+2)%4]);
								}
								if(fx == 0)
								{
									if (ts_x>575 && ts_x<715 && ts_y>125 && ts_y<192)
									{
										show_bmp("./4.bmp");
										flag_show_char = 1;
										musicflag=music_switch%4;
										flag_gc = musicflag%3;
										play_music(musicname[music_switch%4]);
										show_string(musicname1[music_switch%4],2);

										ret = pthread_create(&thread_id, NULL, thread_function, (void *)musicname[4]);
										if(ret != 0){
											printf("Create thread error! \n");
											exit(1);
										}

										ret = pthread_create(&thread_id, NULL, thread_function2, (void *)musicname[4]);
										if(ret != 0){
											printf("Create thread error! \n");
											exit(1);
										}
										break;
									}
									if (ts_x>575 && ts_x<715 && ts_y>210 && ts_y<300)
									{
										show_bmp("./4.bmp");
										flag_show_char = 1;
										musicflag=(music_switch+1)%4;
										play_music(musicname[(music_switch+1)%4]);
										show_string(musicname1[(music_switch+1)%4],2);
										flag_gc = musicflag%3;
										ret = pthread_create(&thread_id, NULL, thread_function, (void *)musicname[4]);
										if(ret != 0){
											printf("Create thread error! \n");
											exit(1);
										}
										ret = pthread_create(&thread_id, NULL, thread_function2, (void *)musicname[4]);
										if(ret != 0){
											printf("Create thread error! \n");
											exit(1);
										}
										break;
									}
									if (ts_x>575 && ts_x<715 && ts_y>310 && ts_y<375)
									{
										show_bmp("./4.bmp");
										flag_show_char = 1;
										musicflag=(music_switch+2)%4;
										play_music(musicname[(music_switch+2)%4]);
										show_string(musicname1[(music_switch+2)%4],2);
										flag_gc = musicflag%3;
										ret = pthread_create(&thread_id, NULL, thread_function, (void *)musicname[4]);
										if(ret != 0){
											printf("Create thread error! \n");
											exit(1);
										}
										ret = pthread_create(&thread_id, NULL, thread_function2, (void *)musicname[4]);
										if(ret != 0){
											printf("Create thread error! \n");
											exit(1);
										}
										break;
									}
								}
							}
						}

					}
				}
			}
		}



		/*

		*/

		//二、视频播放器
		//2、获取触摸屏的坐标值
		

		if (ts_x>250 && ts_x<356 && ts_y>141 && ts_y<259)
		{
			mo:
			while (1)
			{
				show_bmp("./6.bmp");
				get_ts_xy(&ts_x, &ts_y);
				if (ts_x>0 && ts_x<50 && ts_y>0 && ts_y<50)
				{
					show_bmp("./1.bmp");
					break;
				}

				if (ts_x>60 && ts_x<520 && ts_y>89 && ts_y<165)
				{
					voswitch = 0;
					voflaggoto =1;
				}
				if (ts_x>60 && ts_x<520 && ts_y>187 && ts_y<263)
				{
					voswitch = 1;
					voflaggoto =1;
				}
				if (ts_x>60 && ts_x<520 && ts_y>279 && ts_y<359)
				{
					voswitch = 2;
					voflaggoto =1;
				}
				if (ts_x>60 && ts_x<520 && ts_y>369 && ts_y<451)
				{
					voswitch = 3;
					voflaggoto =1;
				}

				if(voflaggoto ==1)
				{
					voflaggoto =0;
					play_movie(voname[voswitch%4], 0, 0, 800, 480);
					while (1)
					{
						get_tss(&ts_x, &ts_y,&fx,&double_ts);
						

						if(fx == 1)
						{
							send_cmd_movie(1);
							vo_flag = 1;
							goto start_vo;
						}
						if(fx == 3)
						{
							send_cmd_movie(3);
							continue;
						}
						if(fx == 4)
						{
							send_cmd_movie(5);
							continue;
						}
						if(double_ts == 1)
						{
							send_cmd_movie(4);
							usleep(200000);
							show_bmp_hh("./9.bmp",350,190,100,100);
							continue;
						}

						if(vo_flag == 1)
						{
							start_vo:
							vo_flag =0;
							show_bmp("./11.bmp");
							vo_i = 61;
							
							while (0<vo_i)
							{
								show_bmp_hh("./10.bmp",0,480-62+vo_i,800,62-vo_i);
								vo_i=vo_i-2;
								usleep(20000);
							}
							start_vo1:
							play_movie(voname[voswitch%4], 20, 20, 760, 400);
							while (1)
							{
								get_tss(&ts_x, &ts_y,&fx,&double_ts);
								if (ts_x>387 && ts_x<445 && ts_y>418 && ts_y<480)
								{
									play_movie(voname[voswitch%4], 20, 20, 760, 400);
								}
								if (ts_x>160 && ts_x<215 && ts_y>418 && ts_y<480)
								{
									send_cmd_movie(1);		//退出视频
								}
								if (ts_x>234 && ts_x<300 && ts_y>418 && ts_y<480)
								{
									send_cmd_movie(3);
								}

								if (ts_x>317 && ts_x<365 && ts_y>418 && ts_y<480)
								{
									send_cmd_movie(8);
								}

								// if (ts_x>387 && ts_x<445 && ts_y>418 && ts_y<480)
								// {
								// 	send_cmd_movie(4);
								// }

								if (ts_x>463 && ts_x<518 && ts_y>418 && ts_y<480)
								{
									send_cmd_movie(5);		//暂停/继续视频
								}
								if (ts_x>547 && ts_x<590 && ts_y>418 && ts_y<480)
								{
									send_cmd_movie(6);		//暂停/继续视频
								}
								if (ts_x>607 && ts_x<660 && ts_y>418 && ts_y<480)
								{
									send_cmd_movie(7);		//暂停/继续视频
								}
								if (ts_x>673 && ts_x<747 && ts_y>418 && ts_y<480)
								{
									send_cmd_movie(1);
									show_bmp("./8.bmp");
									show_hemo(voname1[voswitch%4],voname1[(voswitch+1)%4],voname1[(voswitch+2)%4]);
									while(1)
									{
										get_ts(&ts_x,&ts_y,&fx);
										if(fx == 1)
										{
											voswitch++;
											show_hemo(voname1[(voswitch)%4],voname1[(voswitch+1)%4],voname1[(voswitch+2)%4]);
										}
										if(fx == 2)
										{
											voswitch--;
											show_hemo(voname1[(voswitch)%4],voname1[(voswitch+1)%4],voname1[(voswitch+2)%4]);
										}
										if(fx == 0)
										{
											if (ts_x>575 && ts_x<715 && ts_y>125 && ts_y<192)
											{
												voswitch=voswitch%4;
												show_bmp("./7.bmp");
												goto start_vo1;
											}
											if (ts_x>575 && ts_x<715 && ts_y>210 && ts_y<300)
											{
												show_bmp("./7.bmp");
												voswitch=(voswitch+1)%4;
												goto start_vo1;
											}
											if (ts_x>575 && ts_x<715 && ts_y>310 && ts_y<375)
											{
												show_bmp("./7.bmp");
												voswitch=(voswitch+2)%4;
												goto start_vo1;
											}
										}
									}

								}
								if (ts_x>41 && ts_x<101 && ts_y>418 && ts_y<480)
								{
									//show_bmp("./1.bmp");
									send_cmd_movie(1);
									goto mo;
								}
								if(ts_x>0 && ts_x<800 && ts_y>0 && ts_y<405)
								{
									
									if(fx == 2)
									{
										send_cmd_movie(1);
										play_movie(voname[voswitch], 0, 0, 800, 480);
										break;
									}
									if(fx == 3)
									{
										send_cmd_movie(3);
										continue;
									}
									if(fx == 4)
									{
										send_cmd_movie(5);
										continue;
									}
									if(double_ts == 1)
									{
										send_cmd_movie(4);
										usleep(200000);
										show_bmp_hh("./9.bmp",350,190,100,100);
										continue;
									}
									}
								
								}
						}
					}
					
				}


			}
			
			
			
		}
	}

	//close_lcd_color();				//3、关闭显示屏(color)
	close_lcd_bmp();				//3、关闭显示屏(bmp)
	close_ts();						//3、关闭触摸屏
	close(tty1);
	return 0;
}

/*
while (1)
			{
				get_ts_xy(&ts_x, &ts_y);
				if (ts_x>387 && ts_x<445 && ts_y>418 && ts_y<480)
				{
					play_movie("./play.mp4", 20, 20, 780, 400);
				}
				if (ts_x>160 && ts_x<215 && ts_y>418 && ts_y<480)
				{
					send_cmd_movie(1);		//退出视频
				}
				if (ts_x>234 && ts_x<300 && ts_y>418 && ts_y<480)
				{
					send_cmd_movie(3);		//暂停/继续视频
				}
				if (ts_x>314 && ts_x<371 && ts_y>418 && ts_y<480)
				{
					send_cmd_movie(4);		//暂停/继续视频
				}
				if (ts_x>463 && ts_x<518 && ts_y>418 && ts_y<480)
				{
					send_cmd_movie(5);		//暂停/继续视频
				}
				if (ts_x>547 && ts_x<590 && ts_y>418 && ts_y<480)
				{
					send_cmd_movie(6);		//暂停/继续视频
				}
				if (ts_x>607 && ts_x<660 && ts_y>418 && ts_y<480)
				{
					send_cmd_movie(7);		//暂停/继续视频
				}
				if (ts_x>680 && ts_x<738 && ts_y>418 && ts_y<480)
				{
					send_cmd_movie(8);		//暂停/继续视频
				}
				if (ts_x>41 && ts_x<101 && ts_y>418 && ts_y<480)
				{
					show_bmp("./1.bmp");
					break;
				}
			}

*/



// get_ts_xy(&ts_x, &ts_y);
// 		//一、音乐播放器
// 		if(ts_x>250 && ts_x<356 && ts_y>141 && ts_y<259)
// 		{
// 			while (1)
// 			{
// 				if (ts_x>0 && ts_x<400 && ts_y>0 && ts_y<240)
// 				{
// 					play_music("./1.mp3");
// 				}
// 				if (ts_x>400 && ts_x<800 && ts_y>0 && ts_y<240)
// 				{
// 					exit_music();
// 				}
// 				if (ts_x>0 && ts_x<400 && ts_y>240 && ts_y<480)
// 				{
// 					stop_music();
// 				}
// 				if (ts_x>400 && ts_x<800 && ts_y>240 && ts_y<480)
// 				{
// 					cont_music();
// 				}
// 			}
// 		}

// 		//二、视频播放器
// 				//2、获取触摸屏的坐标值
// 		if (ts_x>250 && ts_x<356 && ts_y>141 && ts_y<259)
// 		{
// 			show_bmp("./3.bmp");
// 			while (1)
// 			{
// 				get_ts_xy(&ts_x, &ts_y);
// 				if (ts_x>387 && ts_x<445 && ts_y>418 && ts_y<480)
// 				{
// 					play_movie("./play.mp4", 20, 20, 780, 400);
// 				}
// 				if (ts_x>160 && ts_x<215 && ts_y>418 && ts_y<480)
// 				{
// 					send_cmd_movie(1);		//退出视频
// 				}
// 				if (ts_x>234 && ts_x<300 && ts_y>418 && ts_y<480)
// 				{
// 					send_cmd_movie(3);		//暂停/继续视频
// 				}
// 				if (ts_x>314 && ts_x<371 && ts_y>418 && ts_y<480)
// 				{
// 					send_cmd_movie(4);		//暂停/继续视频
// 				}
// 				if (ts_x>463 && ts_x<518 && ts_y>418 && ts_y<480)
// 				{
// 					send_cmd_movie(5);		//暂停/继续视频
// 				}
// 				if (ts_x>547 && ts_x<590 && ts_y>418 && ts_y<480)
// 				{
// 					send_cmd_movie(6);		//暂停/继续视频
// 				}
// 				if (ts_x>607 && ts_x<660 && ts_y>418 && ts_y<480)
// 				{
// 					send_cmd_movie(7);		//暂停/继续视频
// 				}
// 				if (ts_x>680 && ts_x<738 && ts_y>418 && ts_y<480)
// 				{
// 					send_cmd_movie(8);		//暂停/继续视频
// 				}
// 				if (ts_x>41 && ts_x<101 && ts_y>418 && ts_y<480)
// 				{
// 					show_bmp("./1.bmp");
// 					break;
// 				}
// 			}
// 		}